package com.platform.framework.controller.basedict;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.POIUtils;
import com.platform.common.utils.PageUtils;
import com.platform.framework.controller.BaseController;
import com.platform.framework.entity.basedata.FwPlBaseDict;
import com.platform.framework.service.basedict.FwPlBaseDictService;

@RestController
@RequestMapping("/dict")
public class FwPlBaseDictController extends BaseController{
	
	@Autowired
	FwPlBaseDictService fwBaseDictService;
	
	/**
	 * 文件上传页面
	 * 
	 * @return
	 */
	@RequestMapping("/upload")
	public ModelAndView fileUpload(Model model) {
		ModelAndView mv = new ModelAndView("dict/import");
		return mv;
	}
	
	
	@RequestMapping(value = "importExcel", method = { RequestMethod.POST })
	public String importXls(@RequestParam("excelFile") MultipartFile file){
		//这里得到的是一个集合，里面的每一个元素是String[]数组
		List<Map<String, Object>> list=null;
		try {
			list = POIUtils.readStringExcel(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fwBaseDictService.importXls(list);
		//return null;
	}
	
	
	
	/**
	 * 
	  * 添加数据字典
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return
	 */
	@RequestMapping(value = "add", method = { RequestMethod.POST })
	public String dictSave(HttpServletRequest request){
		String data = request.getParameter("data");
		FwPlBaseDict fwPlBaseDict =  JSONObject.parseObject(data, FwPlBaseDict.class);
		fwPlBaseDict.setId(this.getUUID());
		fwPlBaseDict.setCreateTime(new Date());
		fwPlBaseDict.setCreateUserId(this.getUserCode());
		return fwBaseDictService.saveDict(fwPlBaseDict);
	}
	
	/**
	 * 
	  * 更新数据字典
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return
	 */
	@RequestMapping(value = "update", method = { RequestMethod.POST })
	public String dictUpdate(HttpServletRequest request){
		String data = request.getParameter("data");
		FwPlBaseDict fwPlBaseDict =  JSONObject.parseObject(data, FwPlBaseDict.class);
		fwPlBaseDict.setUpdateTime(new Date());
		fwPlBaseDict.setUpdateUserId(this.getUserCode());
		//fwPlServer.setIsDeleted("0");
		return fwBaseDictService.updateDict(fwPlBaseDict);
	}
	
	/**
	 * 
	  * 删除数据字典
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return
	 */
	@RequestMapping(value = "delete", method = { RequestMethod.DELETE })
	public String dictDelete(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return fwBaseDictService.deleteDict(ids);
	}
	
	/**
	 * 
	  * 根据条件查询所有的数据	
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return dict分页语句
	  * @throws Exception
	  * @createDate 
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "pagelist", method = { RequestMethod.POST })
	public String dictPageList(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("searchPhrase");//查询条件框
		Pageable pageable = null;
		try{
			pageable = PageUtils.dealminiRequest(request);
		}catch (Exception e) {
			e.printStackTrace();
		}
		Page<FwPlBaseDict> pages= null;
	    pages= fwBaseDictService.findDictPage(searchCtx,pageable);
		return JsonUtils.dealMiniPage(pages);
	}
	
	/**
	 * 
	  * 根据条件查询所有的数据	
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return dict分页语句
	  * @throws Exception
	  * @createDate 
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "dictlist", method = { RequestMethod.GET })
	public String dictList(String searchCtx) throws Exception {
		List<FwPlBaseDict> dictLists=null;
		if(StringUtils.isEmpty(searchCtx)){
			dictLists=fwBaseDictService.findAllDictList();
		}else {
			dictLists=fwBaseDictService.findDictList(searchCtx);
		}
		return JsonUtils.dealJson(dictLists);
	}
	
	/**
	 * 
	  * 根据itemCode查询数据	
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @param request
	  * @return dict字典表
	  * @throws Exception
	  * @createDate 
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "itemCodeDictList", method = { RequestMethod.GET })
	public String itemCodeDictList(String itemCode) throws Exception {
		List<FwPlBaseDict> dictLists=null;
		if(StringUtils.isEmpty(itemCode)){
			dictLists=fwBaseDictService.findAllDictList();
		}else {
			dictLists=fwBaseDictService.findDictListByItemCode(itemCode);
		}
		return JsonUtils.dealJson(dictLists);
	}
	
	
	/**
	 * 
	 * 查询所有的字典表数据
	 * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	 * @param request
	 * @return dict字典表
	 * @throws Exception
	 * @createDate 
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "list", method = { RequestMethod.GET })
	public String getList(HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");  
        response.setHeader("Access-Control-Allow-Methods", "POST, GET,DELETE,PUT,OPTIONS");  
        response.setHeader("Access-Control-Allow-Headers", "*");
		List<FwPlBaseDict> dictLists=fwBaseDictService.getAllList();;
		return JsonUtils.dealJson(dictLists);
	}
	
	
	
	
	
	/**
	 * 
	  * 数据字典维护主页
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @return
	 */
    @RequestMapping("/index")  
    public ModelAndView dictIndex() {
    	ModelAndView mv = new ModelAndView("dict/index");
		return mv;
    }
	
	/**
	 * 
	  * 数据字典添加/修改主页
	  * @author 杜天[dutian] Email:13594731440@163.com Tel:17600471932
	  * @return
	 */
    @RequestMapping("/edit")  
    public ModelAndView dictAdd(){
    	ModelAndView mv = new ModelAndView("dict/edit");
		return mv;
    }
}
